// pages/person_info/person_info.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {},
    genders: [{id:1,name:'男'},{id:2,name:'女'}],
    maritals: [{id:1,name:'已婚'},{id:2,name:'未婚'}],
    areas: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('dataTransfer', data => {
      this.setData({
        model: Object.assign({},data.model), //创建一份浅拷贝, 不影响上一页的值
      });
    });
    util.getEducationLevels().then(items=>{
      items.splice(0,1);
      this.setData({educationLevels: items});
    });
    util.getWorkExperienceOptions().then(items=>{
      items.splice(0,1);
      this.setData({workExperienceOptions: items});
    });
    util.getWorkStates().then(items=>{
      this.setData({workStates:items});
	});
	util.getAreasTree(2,false).then(items=>{
		this.setData({areas:items});
	  });
    this.setData({userinfo: app.getUserinfo()});
  },

  bindGenderChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'model.gender': selectedItem.id});
  },

  bindMaritalChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'model.marital': selectedItem.id});
  },

  bindBirthdateChange(event) {
    this.setData({'model.birthdate': event.detail.value});
  },

  bindEducationLevelChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'model.education_level_id': selectedItem.id});
  },

  bindWorkExperienceChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'model.work_experience_id': selectedItem.id});
  },

  bindWorkStateChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'model.current_state_id': selectedItem.id});
  },

  bindHukouCityChange(event) {
    this.setData({'model.hukou_city': event.detail.items[0].name+event.detail.items[1].name});
  },

  bindUploadAvatar() {
    wx.chooseImage({
      count: 1,
      success: res => {
        let file = res.tempFiles[0];
        core.uploadFile({
          filePath: file.path,
          success: (ret, response) => {
            this.setData({'model.avatar': ret.data.url});
          }
        });
      }
    });
  },

  bindSubmit(event) {
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        // 'realname': {required:true},
        // 'gender': {required:true},
        // 'birthdate': {required:true},
        // 'education_level_id': {required:true},
        // 'work_experience_id': {required:true},
        // 'current_state_id': {required:true},
        'idcard_no':{idcard:true},
        'mobile': {mobile:true},
        'email': {email:true}
      },
      messages: {
        // 'realname': {required:'请填写真实姓名'},
        // 'gender': {required:'请选择性别'},
        // 'birthdate': {required:'请选择出生日期'},
        // 'education_level_id': {required:'请选择学历'},
        // 'work_experience_id': {required:'请选择工作年限'},
        // 'current_state_id': {required:'请选择求职状态'},
        'idcard_no':{idcard:'请填写正确的身份证号'},
        'mobile': {mobile:'请填写正确的手机号'},
        'email': {email:'请填写正确的邮箱'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    data.module = 'personal_info';
    core.post({
      url: 'xilujob.user/save_module',
      data: data,
      success: ret => {
        this.getOpenerEventChannel().emit('dataTransfer', {model: ret.data.model, complete_percent:ret.data.complete_percent});
        wx.navigateBack();
      }
    });
  },

});
